﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Web.UI.HtmlControls;
using System.Text;
namespace EDMS
{
    public partial class RecordShow : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //判断用户是否已经登录
                try
                {
                    if (!CMSUser.Current.IsLogin)
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("~/Login.aspx");
                }
                databind();
            }
        }
        void databind()
        {
            //当前选举编号
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl order by fssj desc";
                DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
                gvShow.DataSource = table;
                gvShow.KeyFieldName = "jlbh";
                gvShow.DataBind();
                //模版列设置
                ModelSet(table);

                #region 回访受理情况
                string strsqls = @" select * from (select count(*) as a from dbo.Wtjl where flag = '1') as s,
             (select count(*) as b from dbo.Wtjl where flag = '9') as f,
             (select count(*) as e from dbo.Wtjl where flag = '0') as m,
             (select count(*) as c from dbo.Wtjl where ishf = '1') as q,
            (select count(*) as d from dbo.Wtjl where ishf = '9') as w";

                DataTable dta = new DataTable();
                dta = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsqls);
                DataRow drs = dta.Rows[0];

                lbtnSlYes.Text = drs["a"].ToString() + "  条";        //待受理
                lbtnSlCg.Text = drs["b"].ToString() + "  条";       //受理成功
                lbtnSlNo.Text = drs["e"].ToString() + "  条";       //受理失败
                lbtnHfNo.Text = drs["c"].ToString() + "  条";       //待回访
                lbtnHfYes.Text = drs["d"].ToString() + "  条";       //已回访

                #endregion
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl where xjbh='" + CurrentXjbh + "' order by fssj desc";
                DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
                gvShow.DataSource = table;
                gvShow.KeyFieldName = "jlbh";
                gvShow.DataBind();
                //模版列设置
                ModelSet(table);

                #region 回访受理情况
                string strsqls = @" select * from (select count(*) as a from dbo.Wtjl where flag = '1' and xjbh='" + CurrentXjbh + "') as s," +
             "(select count(*) as b from dbo.Wtjl where flag = '9' and xjbh='" + CurrentXjbh + "') as f," +
             "(select count(*) as e from dbo.Wtjl where flag = '0' and xjbh='" + CurrentXjbh + "') as m," +
             "(select count(*) as c from dbo.Wtjl where ishf = '1' and xjbh='" + CurrentXjbh + "') as q," +
             "(select count(*) as d from dbo.Wtjl where ishf = '9' and xjbh='" + CurrentXjbh + "') as w";

                DataTable dta = new DataTable();
                dta = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsqls);
                DataRow drs = dta.Rows[0];

                lbtnSlYes.Text = drs["a"].ToString() + "  条";        //待受理
                lbtnSlCg.Text = drs["b"].ToString() + "  条";       //受理成功
                lbtnSlNo.Text = drs["e"].ToString() + "  条";       //受理失败
                lbtnHfNo.Text = drs["c"].ToString() + "  条";       //待回访
                lbtnHfYes.Text = drs["d"].ToString() + "  条";       //已回访

                #endregion
            }


        }
        LinkButton getLinkBtnTemplate(int index, string ControlName)
        {
            LinkButton linksl = gvShow.FindHeaderTemplateControl((GridViewDataColumn)gvShow.Columns[0], ControlName) as LinkButton;
            return linksl;
        }
        ASPxButton getAspxBtnTemplate(int index, string ControlName)
        {
            ASPxButton linksl = gvShow.FindRowCellTemplateControl(index, (GridViewDataColumn)gvShow.Columns[10], ControlName) as ASPxButton;
            return linksl;
        }
        //模版列设置
        void ModelSet(DataTable dt)
        {
            //获取模版列的控件值
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ASPxButton linksl = getAspxBtnTemplate(i, "linksl");
                ASPxButton linkhf = getAspxBtnTemplate(i, "linkhf");
                char ishf = Convert.ToChar(dt.Rows[i]["ishf"]);  //是否已回访
                char issl = Convert.ToChar(dt.Rows[i]["issl"]);  //是否已受理  
                string wtzt = dt.Rows[i]["flag"].ToString();     //委托状态
                if (linksl != null && linkhf != null)
                {
                    switch (issl)
                    {
                        case '9':
                            linksl.Text = "受理成功";
                            break;
                        case '1':
                            linksl.Text = "待受理";
                            break;
                        case '0':
                            linksl.Text = "受理失败";
                            break;
                        default:
                            break;
                    }
                    if (wtzt == "1")
                    {
                        linkhf.Text = "请先受理";
                        linkhf.Enabled = false;
                    }
                    else
                    {
                        switch (ishf)
                        {
                            case '9':
                                linkhf.Text = "查看回访";
                                break;
                            case '1':
                                linkhf.Text = "待回访";
                                break;
                            case '0':
                                linkhf.Text = "回访失败";
                                break;
                            default:
                                break;
                        }
                    }
                    if (wtzt == "9" || wtzt == "0")
                    {
                        linksl.Text = "查看受理";
                    }
                    else
                    {
                        linksl.Text = "待受理";
                    }

                }


            }
        }


        //grid   Callback事件
        protected void gvShow_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (wtName.Text.Equals("") && bwtName.Text.Equals("") && cmdflag.Text.Equals(""))
            {
                Frame.Global.GlobalFunctions.Alert("请输入查询条件！");
            }
            else
            {
                if (CMSUser.Current.IsAdmin == true)
                {
                    strSql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where {0} 1=1 order by fssj desc";

                }
                else
                {
                    strSql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where {0} xjbh='" + CurrentXjbh + "'order by fssj desc";
                }

                StringBuilder sb = new StringBuilder();
                List<SqlParameter> paralist = new List<SqlParameter>();

                if (!string.IsNullOrEmpty(wtName.Text))
                {
                    sb.Append("wtname like '%'+ @name +'%' and ");
                    paralist.Add(new SqlParameter("@name", wtName.Text));
                }

                if (!string.IsNullOrEmpty(bwtName.Text))
                {
                    sb.Append("bwtname like '%'+ @bname +'%' and ");
                    paralist.Add(new SqlParameter("@bname", bwtName.Text));
                }

                if (cmdflag.Text != "查看所有" && !string.IsNullOrEmpty(cmdflag.Text))
                {
                    sb.Append("wtfs = @wtfs and ");
                    paralist.Add(new SqlParameter("@wtfs", cmdflag.Value));
                }
                string strsql = string.Format(strSql, sb.ToString());
                DataTable dt = new DataTable();
                dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql, paralist.ToArray());
                gvShow.DataSource = dt;
                gvShow.KeyFieldName = "jlbh";
                gvShow.DataBind();

                //模版列设置
                ModelSet(dt);
            }

        }




        protected void btnClear_Click(object sender, EventArgs e)
        {
            wtName.Text = "";
            bwtName.Text = "";
            cmdflag.Text = "查看所有";
            databind();

        }
        #region 查询
        //待受理   委托状态为待定的
        protected void lbtnSlYes_Click(object sender, EventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where  flag='1' order by fssj desc";
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where xjbh='" + CurrentXjbh + "' and flag='1' order by fssj desc";
            }
            DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
            gvShow.DataSource = table;
            gvShow.KeyFieldName = "jlbh";
            gvShow.DataBind();
            //模版列设置
            ModelSet(table);
        }
        //受理失败
        protected void lbtnSlNo_Click(object sender, EventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where flag='0' order by fssj desc";
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where xjbh='" + CurrentXjbh + "' and flag='0' order by fssj desc";
            }
            DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
            gvShow.DataSource = table;
            gvShow.KeyFieldName = "jlbh";
            gvShow.DataBind();
            //模版列设置
            ModelSet(table);
        }
        //受理成功  委托状态为成功或失败的
        protected void lbtnSlCg_Click(object sender, EventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            string username = Session["username"].ToString();
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where flag='9' order by fssj desc";
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where xjbh='" + CurrentXjbh + "' and flag='9' order by fssj desc";
            }

            DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
            gvShow.DataSource = table;
            gvShow.KeyFieldName = "jlbh";
            gvShow.DataBind();
            //模版列设置
            ModelSet(table);
        }
        //已回访
        protected void lbtnHfYes_Click(object sender, EventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where  ishf='9' order by fssj desc";
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where xjbh='" + CurrentXjbh + "' and ishf='9' order by fssj desc";
            }

            DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
            gvShow.DataSource = table;
            gvShow.KeyFieldName = "jlbh";
            gvShow.DataBind();
            //模版列设置
            ModelSet(table);
        }
        //待回访
        protected void lbtnHfNo_Click(object sender, EventArgs e)
        {
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strsql = "";
            string username = Session["username"].ToString();
            if (CMSUser.Current.IsAdmin == true)
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where ishf='1' order by fssj desc";
            }
            else
            {
                strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl  where xjbh='" + CurrentXjbh + "' and ishf='1' order by fssj desc";
            }

            DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
            gvShow.DataSource = table;
            gvShow.KeyFieldName = "jlbh";
            gvShow.DataBind();
            //模版列设置
            ModelSet(table);
        }
        #endregion

        protected void btnreflsh_Click(object sender, EventArgs e)
        {
            wtName.Text = "";
            bwtName.Text = "";
            databind();
        }

    }
}